
var map = new Mappy.api.map.Map({
    container:'#mymap'
});
map.setCenter(new Mappy.api.geo.Coordinates(-1.664, 48.1142),5);


       
function displayUser(longitude, latitude) {
	monmanager.debug("addUser");
	var markerLayer = new Mappy.api.map.layer.MarkerLayer(40);
	map.addLayer(markerLayer);
	var icon = new Mappy.api.ui.Icon(Mappy.api.ui.Icon.DEFAULT);
	icon.label = 'You';
	var marker = new Mappy.api.map.Marker(new Mappy.api.geo.Coordinates(longitude, latitude), icon);
	markerLayer.addMarker(marker);
}

function addStations(stations) {
	monmanager.debug("add stations");
	//monmanager.debug(stations[0].latitude);

	var markerLayer = new Mappy.api.map.layer.MarkerLayer(40);
	map.addLayer(markerLayer);
	for (i=0;i<stations.length;i++)
	{
		var station = stations[i];
		monmanager.debug("The number is " + i);
		var icon = new Mappy.api.ui.Icon(Mappy.api.ui.Icon.DEFAULT);
		icon.label = station.name;
		var marker = new Mappy.api.map.Marker(new Mappy.api.geo.Coordinates(station.longitude, station.latitude), icon);
		
		marker.addListener("click", function () {
		    marker.openPopUp('<p><b>' + station.name + '</b><br />' + station.bikeCount + '/' + station.slotCount + '</p>');
		});
		
		markerLayer.addMarker(marker);
	}

	
	var bounds = markerLayer.getBounds();
	var zoomLevel = map.getBoundsZoomLevel(bounds);
	

	map.setCenter(bounds.center, zoomLevel);
	
	

	/*
	var markerLayer = new Mappy.api.map.layer.MarkerLayer(40);
	map.addLayer(markerLayer);
	
	var i=0;
	for(i = 0; i < stations.length; i++) {
		var icon = new Mappy.api.ui.Icon(Mappy.api.ui.Icon.DEFAULT);
		icon.label = 'label';
		var marker = new Mappy.api.map.Marker(new Mappy.api.geo.Coordinates(stations[i].longitude, stations[i].latitude)), icon);
		markerLayer.addMarker(marker);
	}*/
}




//map.disableDblClickZoom();

var scalePosition = new Mappy.api.map.tools.ToolPosition('lb', new Mappy.api.types.Point(10, 0));
var scale = new Mappy.api.map.tools.Scale(scalePosition);
map.addTool(scale);

var minimapPosition = new Mappy.api.map.tools.ToolPosition('lb');
var minimap = new Mappy.api.map.tools.MiniMap(minimapPosition);
map.addTool(minimap);
 
var toolBarPosition = new Mappy.api.map.tools.ToolPosition('rb', new Mappy.api.types.Point(10, 15));
/*
var toolBar = new Mappy.api.map.tools.ToolBar({
    miniMap : { label : "Open miniMap",
                  position : minimapPosition },
    move : { label : "Click to move" },
    zoom : { label : "Zoom in/out" },
    mouseWheelZoom : { label : "Enable/Disable zoom on mousewheel" },
    selection : { label: "Rectangle selection" },
    slider : { label: "Slider" }
}, toolBarPosition);
map.addTool(toolBar);
*/
var tools = new Mappy.api.map.tools.ToolBar({
        zoom : true
    }, new Mappy.api.map.tools.ToolPosition('rb', new Mappy.api.types.Point(20, 20))
);
map.addTool(tools);

monmanager.displayUser.connect(displayUser);
monmanager.addStations.connect(addStations);


